package com.fira.edu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fira.common.core.text.Convert;
import com.fira.common.utils.ChineseCharacterUtil;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.edu.domain.EduStudent;
import com.fira.edu.domain.EduStudentSettle;
import com.fira.edu.domain.dto.parent.StudentCourseDTO;
import com.fira.edu.domain.vo.*;
import com.fira.edu.mapper.EduStudentMapper;
import com.fira.edu.mapper.EduStudentSettleMapper;
import com.fira.edu.service.IEduStudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.Collator;
import java.util.*;

/**
 * 学生信息Service业务层处理
 *
 * @author wuzhenggui
 * @date 2021-10-11
 */
@Service
public class EduStudentServiceImpl implements IEduStudentService {
    @Autowired
    private EduStudentMapper eduStudentMapper;
    @Autowired
    private EduStudentSettleMapper eduStudentSettleMapper;

    /**
     * 查询学生信息
     *
     * @param id 学生信息ID
     * @return 学生信息
     */
    @Override
    public EduStudent selectEduStudentById(String id) {
        return eduStudentMapper.selectEduStudentById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param eduStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<EduStudent> selectEduStudentList(EduStudent eduStudent) {
        return eduStudentMapper.selectEduStudentList(eduStudent);
    }

    /**
     * 新增学生信息
     *
     * @param eduStudent 学生信息
     * @return 结果
     */
    @Override
    public int insertEduStudent(EduStudent eduStudent) {
        eduStudent.setId(IdUtils.fastSimpleUUID());
        eduStudent.setCreateTime(DateUtils.getNowDate());
        return eduStudentMapper.insertEduStudent(eduStudent);
    }

    /**
     * 修改学生信息
     *
     * @param eduStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateEduStudent(EduStudent eduStudent) {
        eduStudent.setUpdateTime(DateUtils.getNowDate());
        return eduStudentMapper.updateEduStudent(eduStudent);
    }

    /**
     * 删除学生信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEduStudentByIds(String ids) {
        return eduStudentMapper.deleteEduStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息ID
     * @return 结果
     */
    @Override
    public int deleteEduStudentById(String id) {
        return eduStudentMapper.deleteEduStudentById(id);
    }

    /**
     * 小程序-查询学生课程信息
     *
     * @param studentCourseDTO
     * @return
     */
    @Override
    public List<StudentCourseVO> selectStudentCourses(StudentCourseDTO studentCourseDTO) {
        List<StudentCourseVO> studentCourseVOList = eduStudentMapper.selectStudentCourses(studentCourseDTO);
        if (CollectionUtil.isNotEmpty(studentCourseVOList)) {
            studentCourseVOList.forEach(studentCourseVO -> {
                EduStudentSettle eduStudentSettle = new EduStudentSettle();
                eduStudentSettle.setStudentCourseId(studentCourseVO.getStudentCourseId());
                //查询学生课程购买信息
                List<EduStudentSettle> eduStudentSettleList = eduStudentSettleMapper.selectEduStudentSettleList(eduStudentSettle);
                if (CollectionUtil.isNotEmpty(eduStudentSettleList)) {
                    List<CourseBuyInfoVO> voList = new ArrayList<>();
                    eduStudentSettleList.forEach(item -> {
                        CourseBuyInfoVO courseBuyInfoVO = new CourseBuyInfoVO();
                        BeanUtils.copyProperties(item, courseBuyInfoVO);
                        voList.add(courseBuyInfoVO);
                    });
                    studentCourseVO.setCourseBuyInfoVO(voList);
                }
            });
        }
        return studentCourseVOList;
    }


    @Override
    public List<ParentStudentsVO> selectStudentAndParentList(List<String> phoneList, String studentName,String tenantId) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("list", phoneList);
        map.put("tenantId", tenantId);
        return eduStudentMapper.selectStudentAndParentList(map);
    }

    @Override
    public List<EduStudentVO> selectStudentHistoryList(EduStudent eduStudent) {
        return buildEduStudentList(eduStudentMapper.selectStudentHistoryList(eduStudent));
    }

    @Override
    public List<EduStudentVO> selectStudentOnlineList(EduStudent eduStudent) {
        return buildEduStudentList(eduStudentMapper.selectStudentOnlineList(eduStudent));
    }

    /**
     * 查询学生家长列表信息
     *
     * @param studentId
     * @return
     */
    @Override
    public List<StudentParentsVO> selectStudentParents(String studentId) {
        return eduStudentMapper.selectStudentParents(studentId);
    }

    /**
     * 统计当年每月的营收金额
     *
     * @return
     */
    @Override
    public List<MonthAmountVO> selectMonthAmountOfYear() {
        return eduStudentMapper.selectMonthAmountOfYear();
    }

    /**
     * 统计今日新增的学生数量
     *
     * @return
     */
    @Override
    public Integer selectStuCountOfToday() {
        return eduStudentMapper.selectStuCountOfToday();
    }

    /**
     * 统计租户下的所有学生数量
     *
     * @return
     */
    @Override
    public Integer selectStuCount() {
        return eduStudentMapper.selectStuCount();
    }

    private List<EduStudentVO> buildEduStudentList(List<EduStudentVO> list) {
        if (list == null || list.size() == 0) {
            return  new ArrayList<>();
        }
//        for (EduStudentVO ed : list) {
//            ed.setFirstPrefix(ChineseCharacterUtil.getUpperCase(ed.getStudentName().substring(0, 1), false));
//
//        }

        //按照名称排序
        Collections.sort(list, new Comparator<EduStudentVO>() {
            @Override
            public int compare(EduStudentVO o1, EduStudentVO o2) {
                //排序规则：按照汉字拼音首字母排序
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                o1.setFirstPrefix(ChineseCharacterUtil.getUpperCase(o1.getStudentName().substring(0, 1), false));
                o2.setFirstPrefix(ChineseCharacterUtil.getUpperCase(o2.getStudentName().substring(0, 1), false));
                return com.compare(o1.getStudentName(), o2.getStudentName());
            }
        });



        return list;
    }


}
